#ifndef SECONDSONLYTIMEEDIT_HPP
#define SECONDSONLYTIMEEDIT_HPP

#include <QTimeEdit>

/**
 * @brief 自定义的 QTimeEdit，可以编辑「小时:分钟:秒」三部分
 */
class SecondsOnlyTimeEdit : public QTimeEdit {
    Q_OBJECT

public:
    explicit SecondsOnlyTimeEdit(QWidget* parent = nullptr)
        : QTimeEdit(parent) {
        setDisplayFormat("HH:mm:ss");
        setWrapping(false);
        setAlignment(Qt::AlignCenter);
        setReadOnly(false);
        setButtonSymbols(QAbstractSpinBox::UpDownArrows);
        setCurrentSection(QDateTimeEdit::HourSection);
    }

protected:
    /**
     * @brief 允许小时、分钟、秒都启用上下按钮
     */
    QAbstractSpinBox::StepEnabled stepEnabled() const override {
        QDateTimeEdit::Section section = currentSection();
        if (section == QDateTimeEdit::HourSection ||
                section == QDateTimeEdit::MinuteSection ||
                section == QDateTimeEdit::SecondSection) {
            return QAbstractSpinBox::StepUpEnabled | QAbstractSpinBox::StepDownEnabled;
        }
        return QAbstractSpinBox::StepNone;
    }

    /**
     * @brief 分别处理小时、分钟、秒的步进
     */
    void stepBy(int steps) override {
        QDateTimeEdit::Section section = currentSection();
        QTime t = time();

        if (section == QDateTimeEdit::HourSection) {
            t = t.addSecs(steps * 3600);  // 1 小时 = 3600 秒
        } else if (section == QDateTimeEdit::MinuteSection) {
            t = t.addSecs(steps * 60);    // 1 分钟 = 60 秒
        } else if (section == QDateTimeEdit::SecondSection) {
            t = t.addSecs(steps);
        }

        setTime(t);
    }
};

#endif // SECONDSONLYTIMEEDIT_HPP
